package com.bds.mynotebook.addeditnotes;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;


import com.bds.mynotebook.R;
import com.bds.mynotebook.databinding.ActivityAddNoteBinding;

public class AddNoteActivity extends AppCompatActivity {
//    public static final String EXTRA_ID = BuildConfig.APPLICATION_ID.concat("EXTRA_ID");
//    public static final String EXTRA_TITLE = BuildConfig.APPLICATION_ID.concat("EXTRA_TITLE");
//    public static final String EXTRA_DESC = BuildConfig.APPLICATION_ID.concat("EXTRA_DESC");
//    public static final String EXTRA_PRIORITY = BuildConfig.APPLICATION_ID.concat("EXTRA_PRIORITY");

    public static final String EXTRA_ID = "EXTRA_ID";
    public static final String EXTRA_TITLE = "EXTRA_TITLE";
    public static final String EXTRA_DESC = "EXTRA_DESC";
    public static final String EXTRA_PRIORITY = "EXTRA_PRIORITY";

    private ActionBar actionBar;

    ActivityAddNoteBinding binding;
    private AddNoteViewModel addNoteViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_add_note);
        addNoteViewModel = new AddNoteViewModel();
        binding.setViewmodel(addNoteViewModel);

        initialize();

        Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_ID)) {
            setTitle("Edit Note");
        } else {
            setTitle("Edit Note");
        }
    }

    private void initialize() {
        acquireIds();

        actionBar.setHomeAsUpIndicator(R.drawable.ic_close);
        Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_ID)) {
            setTitle(R.string.edit_note_title);
            addNoteViewModel.setTitle(intent.getStringExtra(EXTRA_TITLE));
            addNoteViewModel.setDescription(intent.getStringExtra(EXTRA_DESC));
            addNoteViewModel.setPriority(intent.getIntExtra(EXTRA_PRIORITY, 1));
        } else {
            setTitle(R.string.add_note_title);
            addNoteViewModel.setTitle("");
            addNoteViewModel.setDescription("");
            addNoteViewModel.setPriority(2);
        }
    }


    private void acquireIds() {
        actionBar = getSupportActionBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.add_note_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_note:
                saveNote();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void saveNote() {

        String titleString = addNoteViewModel.getTitle();
        String desc = addNoteViewModel.getDescription();
        int priority = addNoteViewModel.getPriority();

        if (titleString.trim().isEmpty() || desc.trim().isEmpty()) {
            Toast.makeText(this, "Please insert Title and Description", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent data = new Intent();
        data.putExtra(EXTRA_TITLE, titleString);
        data.putExtra(EXTRA_DESC, desc);
        data.putExtra(EXTRA_PRIORITY, priority);

        int id = getIntent().getIntExtra(EXTRA_ID, -1);
        if (id != -1) {
            data.putExtra(EXTRA_ID, id);
        }

        setResult(RESULT_OK, data); //Indicates that activity's work was success
        finish();
    }
}